package wt.hpad.base;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;

import java.util.List;

/**
 * 滑动 item 的 recyclerview 适配器
 * Created by wt on 12.25.
 */

public abstract class BaseRvSwAdapter<T> extends RecyclerSwipeAdapter {
    public static final int TYPE_NORMAL = 0; // 普通
    public static final int TYPE_SWIPE = 1;  // 带滑动
    public Activity activity;

    private List<T> mDatas;
    private Context mContext;

    public BaseRvSwAdapter(Activity activity, List<T> mDatas, Context mContext) {
        this.activity = activity;
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    public List<T> getDatas() {
        return mDatas;
    }

    protected abstract BaseHolder createViewHolder(Context context, ViewGroup parent, int viewType);

    protected abstract int getItemType(int position);

    protected abstract int getRId(int position);

    public void removeItem(int position) {
        notifyItemRemoved(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return createViewHolder(mContext, parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        BaseHolder itemHolder = (BaseHolder<T>) viewHolder;
        T bean = mDatas.get(position);
        if (getItemViewType(position) == TYPE_NORMAL) {
            itemHolder.bindItemValue(bean);
        } else {
            itemHolder.bindSwItemValue(mItemManger, bean, position);
            mItemManger.bindView(itemHolder.itemView, position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return getItemType(position);
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return getRId(position);
    }
}
